//package com.faner.fast.manager.redis;
//
//
//import cn.hutool.core.util.StrUtil;
//import com.faner.fast.constant.QueueConstants;
//import com.faner.fast.exception.FailedException;
//import com.faner.fast.exception.HttpException;
//import com.faner.fast.util.RedisUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.Redisson;
//import org.redisson.api.RLock;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationEventPublisher;
//import org.springframework.data.redis.connection.Message;
//import org.springframework.data.redis.connection.MessageListener;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.TimeUnit;
//
//@Component
//@Slf4j
//public class TopicMessageListener implements MessageListener {
//    private static ApplicationEventPublisher publisher;
//    private static Redisson redisson;
//    private static RedisUtil redisUtil;
//    @Autowired
//    public void setRedisUtil( RedisUtil redisUtil){
//        this.redisUtil = redisUtil;
//    }
//    @Autowired
//    public void setRedisson(Redisson redisson){
//        this.redisson = redisson;
//    }
//    @Autowired
//    public void setPublisher(ApplicationEventPublisher publisher){
//        TopicMessageListener.publisher = publisher;
//    }
//
//    @Override
//    public void onMessage(Message message, byte[] bytes) {
//        byte[] body = message.getBody();
//        byte[] channel = message.getChannel();
//        String expiredKey = new String(body);
//        String topic = new String(channel);
//        log.info("[TOPIC]:{} ---- body is [{}]", topic, expiredKey);
//        //加锁
//        RLock lock = redisson.getLock(topic + "-" + QueueConstants.PDF_QUEUE);
//        int lockTimeLimit = 30;
//        try{
//            //等价于 lock.lock(), 默认为30s
//            lock.lock(lockTimeLimit, TimeUnit.SECONDS);
//            String[] tmp = StrUtil.split(expiredKey, "|");
//            //如果非指定的过期键 不推送
//            if(tmp.length == 2 && tmp[0].equals("pdf") && StrUtil.isNotBlank(StrUtil.trim(tmp[1]))){
//                redisUtil.lSet(QueueConstants.PDF_QUEUE,tmp[1]);
//                //发布
//    //            TopicMessageListener.publisher.publishEvent(messageBO);
//            }
//        } catch (Exception e) {
//            log.error("Error occurred, lock message:{}", e.getMessage());
//            if (e instanceof HttpException) {
//                throw new FailedException(((HttpException) e).getCode());
//            }
//        } finally {
//            lock.unlock();
////            if(lock.isLocked()) {
////                if (lock.isHeldByCurrentThread()) {
////                    lock.unlock();
////                }
////            }
//        }
//    }
//}
